<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  goods_model.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-18.
//  Copyright 2011 lishuzu. All rights reserved.
// 

class Goods_model extends CI_Model
{
		function __construct(){
			parent::__construct();
			$this->category = $this->config->item('category');
		}
		
		function get_goods($aid, $limit = ''){
			$goods = array();
			$this->db->order_by('dateline', 'desc');
			$this->db->where('aid', $aid);
			if ($limit) {
				$this->db->limit($limit);
			}
			$query = $this->db->get('goods');
			foreach ($query->result_array() as $row) {
				//图片模式切换 0 外链 1上传
				if ($row['img_mode'] == 1) {
					$row['goods_img'] = base_url().'upload/attachments/'.$row['uploadimg'];
				}else{
					$row['goods_img'] = $row['imageurl'];					
				}
				$row['catname'] = $row['catid'] ? $this->category[$row['catid']]['name'] : '未分类';
				$goods[] = $row;
			}
			return $goods;
		}
		
		function get_goods_by_gid($gid){
			if ($gid) {
				$this->db->where('gid', $gid);
				$query = $this->db->get('goods');
				$goods = $query->row_array();
				//图片模式切换 0 外链 1上传
				if ($goods['img_mode'] == 1) {
					$goods['goods_img'] = base_url().'upload/attachments/'.$goods['uploadimg'];
				}else{
					$goods['goods_img'] = $goods['imageurl'];
				}
				return $goods;
			}
			$goods['catname'] = $goods['catid'] ? $this->category[$goods['catid']]['name'] : '未分类';
			return false;
		}
		
		function insert_goods($data){
			if ($data) {
				$this->db->insert('goods', $data);
				return $this->db->insert_id();
			}
			return false;
		}
		
		function update_goods($data){
			if (!empty($data['gid'])) {
				$this->db->where('gid', $data['gid']);
				$this->db->update('goods', $data);
				return $this->db->affected_rows();
			}
			return false;
		}
		
		function deletes_goods($ids){
			if($ids){
				$this->db->where_in('gid', $ids);
				$this->db->limit(COUNT($ids));
				$this->db->delete('goods');
				if($this->db->affected_rows() <= 0){
					return false;
				}
				return true;
			}
			return false;
		}
}
?>